/*
 * Copyright (c) 2009-2010 Microelectronic Center, 
 * Harbin Institute of Technology
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Gou Pengfei
 */

#ifndef __EDGE_SIM_POINT_HH__
#define __EDGE_SIM_POINT_HH__

#include <string>

#include "base/types.hh"
#include "sim/system.hh"
#include "sim/serialize.hh"

/**
 * This is an EDGE specific checkpoint.
 * Due to the published articles of TRIPS
 * group, simpoints are gathered with 
 * accordance to the numbers of specific 
 * function calls and returns. For details 
 * please refer to the Phd. thesis of 
 * Ramadass Nagarajan.
 */

template<class Impl>
class EdgeSimPoint
{
  public:

    typedef typename Impl::EdgeBlockPtr EdgeBlockPtr;

    EdgeSimPoint(std::string &start_sym, std::string &end_sym, 
            int call_num, int ret_num, std::string &mode);

    EdgeSimPoint();

    ~EdgeSimPoint();

    bool checkStartPoint(EdgeBlockPtr &edge_block);
    bool checkEndPoint(EdgeBlockPtr &edge_block);

    inline bool isValid()
    {
        if (_mode == "StartPoint") {
            return (_callNum > 0 && 
                    _startFunctionName.size() > 0 &&
                    _startPointAddr > 0);
        } else if (_mode == "EndPoint") {
            return (_retNum > 0 &&
                    _endFunctionName.size() > 0 &&
                    _endPointAddrMax > 0 &&
                    _endPointAddrMin > 0 &&
                    (_endPointAddrMin <= _endPointAddrMax));
        } else {
            return false;
        }
    }

    void setRetNumBeforeStart(int num)
    { 
        assert(_mode == "EndPoint");
        retNumBeforeStart = num; 
        dynNum = num;
    }

    int getRetNumBeforeStart() const { return retNumBeforeStart; }

    void serialize(std::ostream &os);
    void unserialize(Checkpoint *cp, const std::string &section);

  private:

    /// The function name of the start simpoint.
    std::string _startFunctionName;

    /// The function name of the end simpoint.
    std::string _endFunctionName;

    /// The address of the start point
    Addr _startPointAddr;

    /// The address of the end point
    Addr _endPointAddrMin;
    Addr _endPointAddrMax;

    /// Current mode of this point
    std::string _mode;

    /// The number of calls of start simpoint.
    int _callNum;

    /// The number of returns of end simpoint.
    int _retNum;

    /// Dynamic numbers of the specific point.
    int dynNum;

    /// Number of rets before the start point.
    int retNumBeforeStart;
}; 

#endif /// __EDGE_SIM_POINT_HH__

